****************************************************************************
					Replication file for 
		Electoral integrity matters: how electoral process conditions 
		the relationship between political losing and political trust
					Marlene Mauk
					version: 14 August 2020
****************************************************************************

*******************************
instructions
1. download the following data sets 
Asian Barometer Wave 3 (from www.asianbarometer.org)
European Social Survey Wave 6 (from europeansocialsurvey.org)
Latinobarómetro 2013 (from latinobarometro.org)
Varieties-of-Democracy Project v9 (from v-dem.net)
file versions are indicated below, but code should work with other versions as well
2. execute the Stata do file up unto line 662
3. execute the Mplus input files as indicated in lines 663-670
4. execute the rest of the Stata code
*******************************

cd [path]


*****************************************
	recoding survey and macro data
*****************************************

** Asian Barometer
use "[path]\Asian_3_merge_v20130904.dta"

tab country
gen countrymerge = country + 400
label variable countrymerge "country"
label drop country
label define country 
401 "Japan 2011 (Asian)" 402 "Hong Kong 2012 (Asian)"  403 "South Korea 2011 (Asian)" 404 "China 2011 (Asian)" 405 "Mongolia 2010 (Asian)" 406 "Philippines 2010 (Asian)" ///
407 "Taiwan 2010 (Asian)" 408 "Thailand 2010 (Asian)" 409 "Indonesia 2011 (Asian)" 410 "Singapore 2010 (Asian)" 411 "Vietnam 2010 (Asian)" 412 "Cambodia 2012 (Asian)" 413 "Malaysia 2011 (Asian)" ///
501 "Argentina 2012 (Latino)" 502 "Bolivia 2013 (Latino)" 503 "Brazil 2013 (Latino)" 504 "Chile 2013 (Latino)" 505 "Colombia 2013 (Latino)" 506 "Costa Rica 2013 (Latino)" 507 "Dominican Republic 2013 (Latino)" ///
508 "Ecuador 2013 (Latino)" 509 "El Salvador 2013 (Latino)" 510 "Guatemala 2013 (Latino)" 511 "Honduras 2013 (Latino)" 512 "Mexico 2013 (Latino)" 513 "Nicaragua 2013 (Latino)" 514 "Panama 2013 (Latino)" ///
515 "Paraguay 2013 (Latino)" 516 "Peru 2013 (Latino)" 517 "Uruguay 2013 (Latino)" 518 "Venezuela 2013 (Latino)" 519 "Spain 2013 (Latino)" ///
701 "Albania (ESS)" 702 "Austria (ESS)" 703 "Belgium (ESS)" 704 "Bulgaria (ESS)" 705 "Croatia (ESS)" 706 "Cyprus (ESS)" 707 "Czechia (ESS)" 708 "Denmark (ESS)" 709 "Estonia (ESS)" 710 "Finland (ESS)" 711 "France (ESS)" ///
712 "Germany (ESS)" 713 "Greece (ESS)" 714 "Hungary (ESS)" 715 "Iceland (ESS)" 716 "Ireland (ESS)" 717 "Israel (ESS)" 718 "Italy (ESS)" 719 "Kosovo (ESS)" 720 "Latvia (ESS)" 721 "Lithuania (ESS)" 722 "Luxembourg (ESS)" ///
723 "Montenegro (ESS)" 724 "Netherlands (ESS)" 725 "Norway (ESS)" 726 "Poland (ESS)" 727 "Portugal (ESS)" 728 "Romania (ESS)" 729 "Russia (ESS)" 730 "Serbia (ESS)" 731 "Slovakia (ESS)" 732 "Slovenia (ESS)" ///
733 "Spain (ESS)" 734 "Sweden (ESS)" 735 "Switzerland (ESS)" 736 "Turkey (ESS)" 737 "Ukraine (ESS)" 738 "United Kingdom (ESS)"
label values countrymerge country
tab countrymerge, label

gen year = year(ir9)
gen month = month(ir9)
tab countrymerge if year==. /*interview date is missing for singapore but no problem b/c no democracy anyway*/

tab year month if countrymerge==401
tab year month if countrymerge==402
tab year month if countrymerge==403
tab year month if countrymerge==404
tab year month if countrymerge==405
tab year month if countrymerge==406
tab year month if countrymerge==407
tab year month if countrymerge==408
tab year month if countrymerge==409
tab year month if countrymerge==410
tab year month if countrymerge==411
tab year month if countrymerge==412
tab year month if countrymerge==413

replace year = 2009 if countrymerge==406 /*change matching year for philippines b/c they had elections following the abs survey fieldwork period in 2010*/

gen survey = 4
label define survey 4 "Asian Barometer" 5 "Latinobarometro" 7 "European Social Survey"
label values survey survey
tab survey

mvdecode q11 q14 q8 q10, mv(7 8 9)
gen trust2 = 1-((q11-1)/3)
gen trust3 = 1-((q14-1)/3)
gen trust5 = 1-((q8-1)/3)
gen trust6 = 1-((q10-1)/3)
lab var trust2 "trust in parliament"
lab var trust3 "trust in police"
lab var trust5 "trust in courts"
lab var trust6 "trust in parties"
lab define trust 0 "none at all" 1 "a great deal of trust"
lab val trust2 trust3 trust5 trust6 trust

mvdecode q37, mv(0 5 6 7 8 9)
gen dpe4 = 1-((q37-1)/3)
lab var dpe4 "Freeness and fairness of last national election"
lab define dpe4 0 "not free and fair" 1 "completely free and fair"
lab val dpe4 dpe4

mvdecode q1, mv(7 8 9)
gen spe2 = 1-((q1-1)/4)
lab var spe2 "How would you rate the country's present economic situation?"
lab define spe2 0 "very bad" 1 "very good"
lab val spe2 spe2

mvdecode q95, mv (7 8 9)
gen as1 = 1-((q95-1)/3)
lab var as1 "How satisfied or dissatisfied are you with the [name of present] government?"
lab define as1 0 "very dissatisfied" 1 "very satisfied"
lab val as1 as1

recode se5 (1=1) (2=2) (3=2) (4=3) (5=3) (6=3) (7=3) (8=4) (9=4) (10=4), gen(edulevel)
lab define edulevel 1 "none" 2 "(some) primary" 3 "(some) secondary" 4 "(some) tertiary"
lab val edulevel edulevel
mvdecode edulevel, mv(99)
tab edulevel

mvdecode se12, mv (90 91 92 93 94 95 96 97 98 99)
gen status1 = (se12-1)/9
lab var status1 "subjective socioeconomic status"
lab define status1 0 "lowest status" 1 "highest status"
lab val status1 status1

mvdecode q43, mv(7 8 9)
gen polint = 1-((q43-1)/3)
lab var polint "interest in politics"
lab define polint 0 "not at all interested" 1 "very interested"
lab val polint polint

recode q23 (1=1) (2=0) (6=0.5) (else=.), gen(soctrust)
lab var soctrust "Most people can be trusted"
lab val soctrust yn

recode se2 (1=0) (2=1) (else=.), gen(female)
lab var female "female"
lab val female yn
mvdecode se3a, mv(9)
gen age = se3a

mvdecode q33a, mv(0 8)
recode q33a (1=1) (2=0), gen(winner)
lab var winner "Voted for the winning camp"
lab val winner yn

drop q1-MAweight
drop couweight level3

save asian3.dta, replace


** Latinobarómetro
use "[path]\Latino_2013_merge.dta"

recode idenpa (32=1) (68=2) (76=3) (152=4) (170=5) (188=6) (214=7) (218=8) (222=9) (320=10) (340=11) (484=12) (558=13) (591=14) (600=15) (604=16) (858=17) (862=18) (724=19), gen(country)
tab country
gen countrymerge = country + 500
label define country 401 "Japan 2011 (Asian)" 402 "Hong Kong 2012 (Asian)"  403 "South Korea 2011 (Asian)" 404 "China 2011 (Asian)" 405 "Mongolia 2010 (Asian)" 406 "Philippines 2010 (Asian)" ///
407 "Taiwan 2010 (Asian)" 408 "Thailand 2010 (Asian)" 409 "Indonesia 2011 (Asian)" 410 "Singapore 2010 (Asian)" 411 "Vietnam 2010 (Asian)" 412 "Cambodia 2012 (Asian)" 413 "Malaysia 2011 (Asian)" ///
501 "Argentina 2012 (Latino)" 502 "Bolivia 2013 (Latino)" 503 "Brazil 2013 (Latino)" 504 "Chile 2013 (Latino)" 505 "Colombia 2013 (Latino)" 506 "Costa Rica 2013 (Latino)" 507 "Dominican Republic 2013 (Latino)" ///
508 "Ecuador 2013 (Latino)" 509 "El Salvador 2013 (Latino)" 510 "Guatemala 2013 (Latino)" 511 "Honduras 2013 (Latino)" 512 "Mexico 2013 (Latino)" 513 "Nicaragua 2013 (Latino)" 514 "Panama 2013 (Latino)" ///
515 "Paraguay 2013 (Latino)" 516 "Peru 2013 (Latino)" 517 "Uruguay 2013 (Latino)" 518 "Venezuela 2013 (Latino)" 519 "Spain 2013 (Latino)" ///
701 "Albania (ESS)" 702 "Austria (ESS)" 703 "Belgium (ESS)" 704 "Bulgaria (ESS)" 705 "Croatia (ESS)" 706 "Cyprus (ESS)" 707 "Czechia (ESS)" 708 "Denmark (ESS)" 709 "Estonia (ESS)" 710 "Finland (ESS)" 711 "France (ESS)" ///
712 "Germany (ESS)" 713 "Greece (ESS)" 714 "Hungary (ESS)" 715 "Iceland (ESS)" 716 "Ireland (ESS)" 717 "Israel (ESS)" 718 "Italy (ESS)" 719 "Kosovo (ESS)" 720 "Latvia (ESS)" 721 "Lithuania (ESS)" 722 "Luxembourg (ESS)" ///
723 "Montenegro (ESS)" 724 "Netherlands (ESS)" 725 "Norway (ESS)" 726 "Poland (ESS)" 727 "Portugal (ESS)" 728 "Romania (ESS)" 729 "Russia (ESS)" 730 "Serbia (ESS)" 731 "Slovakia (ESS)" 732 "Slovenia (ESS)" ///
733 "Spain (ESS)" 734 "Sweden (ESS)" 735 "Switzerland (ESS)" 736 "Turkey (ESS)" 737 "Ukraine (ESS)" 738 "United Kingdom (ESS)"
label values countrymerge country

gen survey = 5
lab define survey 4 "Asian Barometer" 5 "Latinobarometro" 7 "European Social Survey"
lab val survey survey

gen year = 2013
replace year=2012 if countrymerge==501 /*replacing matching year b/c countries held elections following fieldwork in the fieldwork year*/
replace year=2012 if countrymerge==504

tab mesreal if countrymerge==501
tab mesreal if countrymerge==502
tab mesreal if countrymerge==503
tab mesreal if countrymerge==504
tab mesreal if countrymerge==505
tab mesreal if countrymerge==506
tab mesreal if countrymerge==507
tab mesreal if countrymerge==508
tab mesreal if countrymerge==509
tab mesreal if countrymerge==510
tab mesreal if countrymerge==511
tab mesreal if countrymerge==512
tab mesreal if countrymerge==513
tab mesreal if countrymerge==514
tab mesreal if countrymerge==515
tab mesreal if countrymerge==516
tab mesreal if countrymerge==517
tab mesreal if countrymerge==518

tab mesreal if countrymerge==504
tab mesreal if countrymerge==508
tab mesreal if countrymerge==515 /*no problem with election dates*/

mvdecode _all, mv(-4 -2 -1)

gen trust2 = 1-((P26TGB_C-1)/3)
gen trust3 = 1-((P28TGB_B-1)/3)
gen trust5 = 1-((P26TGB_E-1)/3)
gen trust6 = 1-((P26TGB_G-1)/3)
lab var trust2 "trust in parliament"
lab var trust3 "trust in police"
lab var trust5 "trust in courts"
lab var trust6 "trust in parties"
lab define trust 0 "none at all" 1 "a great deal of trust" 
lab val trust2 trust3 trust5 trust6 trust

mvdecode P25STGBS, mv(-4 -3 -2 -1)
gen dpe4 = 1-((P25STGBS-1)/4)
lab var dpe4 "Freeness and fairness of last national election"
lab define dpe4 0 "not free and fair" 1 "completely free and fair"
lab val dpe4 dpe4

mvdecode P3STGBS, mv(-4 -2 -1)
gen spe2 = 1-((P3STGBS-1)/4)
lab var spe2 "How would you rate the country's present economic situation?"
lab define spe2 0 "very bad" 1 "very good"
lab val spe2 spe2

gen as2 = 1-(P38STGBS-1)
lab var as2 "Performance of government"
lab define as2 0 "strongly disapprove" 1 "strongly approve"
lab val as2 as2

recode S17 (1=1) (2=2) (3=2) (4=2) (5=2) (6=2) (7=2) (8=3) (9=3) (10=3) (11=3) (12=3) (13=3) (14=4) (15=4) (16=3) (17=3), gen(edulevel)
lab define edulevel 1 "none" 2 "(some) primary" 3 "(some) secondary" 4 "(some) tertiary"
lab val edulevel edulevel

gen status1 = 1-((S8-1)/4)
lab var status1 "subjective socioeconomic status"
lab define status1 0 "lowest status" 1 "highest status"
lab val status1 status1

gen polint = 1-((P20STGBS-1)/3)
lab var polint "interest in politics"
lab define polint 0 "not at all interested" 1 "very interested"
lab val polint polint

recode P29STGBS(1=1) (2=0) (else=.), gen(soctrust)
lab var soctrust "Most people can be trusted"
lab val soctrust yn

recode S10 (1=0) (2=1) (else=.), gen(female)
lab val female yn
tab female
gen age = S11
tab age

mvdecode P23TGBMA, mv(-3)
recode P23TGBMA (1=1) (2=0), gen(winner)
lab var winner "Voted for the winning camp"
lab val winner yn

drop numinves idenpa
drop reg-fampart

save latino2013.dta, replace


** European Social Survey
use "[path]\ESS6e02_2.dta"
encode cntry, gen(country)
gen countryid=.
replace countryid=701 if cntry=="AL"
replace countryid=702 if cntry=="AT"
replace countryid=703 if cntry=="BE"
replace countryid=704 if cntry=="BG"
replace countryid=705 if cntry=="HR"
replace countryid=706 if cntry=="CY"
replace countryid=707 if cntry=="CZ"
replace countryid=708 if cntry=="DK"
replace countryid=709 if cntry=="EE"
replace countryid=710 if cntry=="FI"
replace countryid=711 if cntry=="FR"
replace countryid=712 if cntry=="DE"
replace countryid=713 if cntry=="GR"
replace countryid=714 if cntry=="HU"
replace countryid=715 if cntry=="IS"
replace countryid=716 if cntry=="IE"
replace countryid=717 if cntry=="IL"
replace countryid=718 if cntry=="IT"
replace countryid=719 if cntry=="XK"
replace countryid=720 if cntry=="LV"
replace countryid=721 if cntry=="LT"
replace countryid=722 if cntry=="LU"
replace countryid=723 if cntry=="ME"
replace countryid=724 if cntry=="NL"
replace countryid=725 if cntry=="NO"
replace countryid=726 if cntry=="PL"
replace countryid=727 if cntry=="PT"
replace countryid=728 if cntry=="RO"
replace countryid=729 if cntry=="RU"
replace countryid=730 if cntry=="RS"
replace countryid=731 if cntry=="SK"
replace countryid=732 if cntry=="SI"
replace countryid=733 if cntry=="ES"
replace countryid=734 if cntry=="SE"
replace countryid=735 if cntry=="CH"
replace countryid=736 if cntry=="TR"
replace countryid=737 if cntry=="UA"
replace countryid=738 if cntry=="GB"

*check fieldwork dates to cross-tab with election dates --> select appropriate vdem year *
tab1 inwmme inwyye if countryid==701
tab1 inwmme inwyye if countryid==702
tab1 inwmme inwyye if countryid==703
tab1 inwmme inwyye if countryid==704
tab1 inwmme inwyye if countryid==705
tab1 inwmme inwyye if countryid==706
tab1 inwmme inwyye if countryid==707
tab1 inwmme inwyye if countryid==708
tab1 inwmme inwyye if countryid==709
tab1 inwmme inwyye if countryid==710
tab1 inwmme inwyye if countryid==711
tab1 inwmme inwyye if countryid==712
tab1 inwmme inwyye if countryid==713
tab1 inwmme inwyye if countryid==714
tab1 inwmme inwyye if countryid==715
tab1 inwmme inwyye if countryid==716
tab1 inwmme inwyye if countryid==717
tab1 inwmme inwyye if countryid==718
tab1 inwmme inwyye if countryid==719
tab1 inwmme inwyye if countryid==720
tab1 inwmme inwyye if countryid==721
tab1 inwmme inwyye if countryid==722
tab1 inwmme inwyye if countryid==723
tab1 inwmme inwyye if countryid==724
tab1 inwmme inwyye if countryid==725
tab1 inwmme inwyye if countryid==726
tab1 inwmme inwyye if countryid==727
tab1 inwmme inwyye if countryid==728
tab1 inwmme inwyye if countryid==729
tab1 inwmme inwyye if countryid==730
tab1 inwmme inwyye if countryid==731
tab1 inwmme inwyye if countryid==732
tab1 inwmme inwyye if countryid==733
tab1 inwmme inwyye if countryid==734
tab1 inwmme inwyye if countryid==735
tab1 inwmme inwyye if countryid==736
tab1 inwmme inwyye if countryid==737
tab1 inwmme inwyye if countryid==738

gen year = 2012
replace year=2013 if countryid==708
replace year=2013 if countryid==711
replace year=2013 if countryid==718
replace year=2013 if countryid==721
replace year=2013 if countryid==733
replace year=2013 if countryid==737

rename countryid countrymerge
lab drop country
label define country 401 "Japan 2011 (Asian)" 402 "Hong Kong 2012 (Asian)"  403 "South Korea 2011 (Asian)" 404 "China 2011 (Asian)" 405 "Mongolia 2010 (Asian)" 406 "Philippines 2010 (Asian)" ///
407 "Taiwan 2010 (Asian)" 408 "Thailand 2010 (Asian)" 409 "Indonesia 2011 (Asian)" 410 "Singapore 2010 (Asian)" 411 "Vietnam 2010 (Asian)" 412 "Cambodia 2012 (Asian)" 413 "Malaysia 2011 (Asian)" ///
501 "Argentina 2012 (Latino)" 502 "Bolivia 2013 (Latino)" 503 "Brazil 2013 (Latino)" 504 "Chile 2013 (Latino)" 505 "Colombia 2013 (Latino)" 506 "Costa Rica 2013 (Latino)" 507 "Dominican Republic 2013 (Latino)" ///
508 "Ecuador 2013 (Latino)" 509 "El Salvador 2013 (Latino)" 510 "Guatemala 2013 (Latino)" 511 "Honduras 2013 (Latino)" 512 "Mexico 2013 (Latino)" 513 "Nicaragua 2013 (Latino)" 514 "Panama 2013 (Latino)" ///
515 "Paraguay 2013 (Latino)" 516 "Peru 2013 (Latino)" 517 "Uruguay 2013 (Latino)" 518 "Venezuela 2013 (Latino)" 519 "Spain 2013 (Latino)" ///
701 "Albania (ESS)" 702 "Austria (ESS)" 703 "Belgium (ESS)" 704 "Bulgaria (ESS)" 705 "Croatia (ESS)" 706 "Cyprus (ESS)" 707 "Czechia (ESS)" 708 "Denmark (ESS)" 709 "Estonia (ESS)" 710 "Finland (ESS)" 711 "France (ESS)" ///
712 "Germany (ESS)" 713 "Greece (ESS)" 714 "Hungary (ESS)" 715 "Iceland (ESS)" 716 "Ireland (ESS)" 717 "Israel (ESS)" 718 "Italy (ESS)" 719 "Kosovo (ESS)" 720 "Latvia (ESS)" 721 "Lithuania (ESS)" 722 "Luxembourg (ESS)" ///
723 "Montenegro (ESS)" 724 "Netherlands (ESS)" 725 "Norway (ESS)" 726 "Poland (ESS)" 727 "Portugal (ESS)" 728 "Romania (ESS)" 729 "Russia (ESS)" 730 "Serbia (ESS)" 731 "Slovakia (ESS)" 732 "Slovenia (ESS)" ///
733 "Spain (ESS)" 734 "Sweden (ESS)" 735 "Switzerland (ESS)" 736 "Turkey (ESS)" 737 "Ukraine (ESS)" 738 "United Kingdom (ESS)"
label values countrymerge country

gen survey = 7
lab define survey 4 "Asian Barometer" 5 "Latinobarometro" 7 "European Social Survey"
lab val survey survey

mvdecode trstprl trstlgl trstplc trstprt, mv(77 88 99)
gen trust2 = trstprl/10
gen trust3 = trstplc/10
gen trust5 = trstlgl/10
gen trust6 = trstprt/10

lab var trust2 "trust in parliament"
lab var trust3 "trust in police"
lab var trust5 "trust in courts"
lab var trust6 "trust in parties"
lab define trust 0 "none at all" 1 "a great deal of trust" 
lab val trust2 trust3 trust5 trust6 trust
tab trust8

tab fairelcc
mvdecode fairelcc, mv(77 88 99)
gen dpe4 = fairelcc/10
lab var dpe4 "Freeness and fairness of last national election"
lab define dpe4 0 "not free and fair" 1 "completely free and fair"
lab val dpe4 dpe4

tab stfeco, nolabel
mvdecode stfeco, mv(77 88 99)
gen spe2 = stfeco/10
lab var spe2 "How would you rate the country's present economic situation?"
lab define spe2 0 "very bad" 1 "very good"
lab val spe2 spe2

mvdecode stfgov, mv(77 88 99)
gen as1 = stfgov/10
lab var as1 "How satisfied or dissatisfied are you with the [name of present] government?"
lab define as1 0 "very dissatisfied" 1 "very satisfied"
lab val as1 as1

tab edulvlb
recode edulvlb (0=1) (113=2) (129/323=3) (412/800=4) (else=.), gen(edulevel)
lab define edulevel 1 "none" 2 "(some) primary" 3 "(some) secondary" 4 "(some) tertiary"
lab val edulevel edulevel

mvdecode plinsoc, mv(77 88 99)
gen status1 = plinsoc/10
lab var status1 "subjective socioeconomic status"
lab define status1 0 "lowest status" 1 "highest status"
lab val status1 status1
tab status1

mvdecode polintr, mv(7 8 9)
gen polint = 1-((polintr-1)/3)
lab var polint "interest in politics"
lab define polint 0 "not at all interested" 1 "very interested"
lab val polint polint

mvdecode ppltrst, mv(77 88 99)
gen soctrust = ppltrst/10
lab var soctrust "Most people can be trusted"
lab val soctrust yn

mvdecode gndr, mv(9)
mvdecode agea, mv(999)
recode gndr (1=0) (2=1) (else=.), gen(female)
lab val female yn
gen age = agea


*generate variable giving the year of the last election
gen eleyear = .
replace eleyear = 2010 if cntry == "BE" & essround == 6 
replace eleyear = 2011 if cntry == "DK" & essround == 6 
replace eleyear = 2011 if cntry == "FI" & essround == 6 
replace eleyear = 2012 if cntry == "FR" & essround == 6 
replace eleyear = 2009 if cntry == "DE" & essround == 6 
replace eleyear = 2011 if cntry == "IE" & essround == 6 
replace eleyear = 2013 if cntry == "IT" & essround == 6 
*replace eleyear = 2012 if cntry == "NL" & essround == 6 //*they (Netherlands) had elections during ESS fieldwork period --> exclude*//
replace eleyear = 2009 if cntry == "NO" & essround == 6 
replace eleyear = 2011 if cntry == "PT" & essround == 6 
replace eleyear = 2011 if cntry == "ES" & essround == 6 
replace eleyear = 2010 if cntry == "SE" & essround == 6 
replace eleyear = 2011 if cntry == "CH" & essround == 6 
replace eleyear = 2010 if cntry == "GB" & essround == 6 
replace eleyear = 2009 if cntry == "IS" & essround == 6 
replace eleyear = 2009 if cntry == "BG" & essround == 6
replace eleyear = 2011 if cntry == "CY" & essround == 6
replace eleyear = 2010 if cntry == "BE" & essround == 6
replace eleyear = 2011 if cntry == "EE" & essround == 6
replace eleyear = 2010 if cntry == "HU" & essround == 6
*replace eleyear = 2009 if cntry == "IL" & essround == 6 //*they (Israel) had elections during ESS fieldwork period --> exclude*//
replace eleyear = 2012 if cntry == "LT" & essround == 6
replace eleyear = 2011 if cntry == "PL" & essround == 6
replace eleyear = 2011 if cntry == "SI" & essround == 6
replace eleyear = 2012 if cntry == "SK" & essround == 6
replace eleyear = 2012 if cntry == "UA" & essround == 6
replace eleyear = 2009 if cntry == "AL" & essround == 6
replace eleyear = 2010 if cntry == "CZ" & essround == 6
tab cntry if eleyear==.

*create variable indicating whether a respondent voted for a party that ended up in government
gen winner = .
* Albania 
replace winner = 1 if prtvtal == 2 | prtvtal == 3 | prtvtal == 4 | prtvtal == 6
replace winner = 0 if prtvtal == 1 | prtvtal == 5 | prtvtal == 7 /*exclude "Others" because they might be members of coalition government*/
* Belgium
replace winner = 1 if prtvtcbe == 13 & eleyear == 2010 | prtvtcbe == 12 | prtvtcbe == 2 | prtvtcbe == 9 & eleyear == 2010 | prtvtcbe == 8 | prtvtcbe == 5 & eleyear == 2010 
replace winner = 0 if winner == . & prtvtcbe < 19
* Bulgaria
replace winner = 1 if prtvtcbg == 1
replace winner = 0 if winner == . & prtvtcbg <= 13 /*include "Others" because no coalition government*/
* Cyprus
replace winner = 1 if prtvtacy == 1
replace winner = 0 if winner == . & prtvtacy <=7 /*include "Others" because no coalition government*/
* Czechia
replace winner = 1 if prtvtccz == 3 | prtvtccz == 4 | prtvtccz == 5
replace winner = 0 if prtvtccz == 1 | prtvtccz == 2 | prtvtccz == 8 /*include "Others" because 3-party coalition*/
* Denmark
replace winner = 1 if prtvtcdk == 1 | prtvtcdk == 2 | prtvtcdk == 4
replace winner = 0 if winner == . & prtvtcdk < 11
* Estonia
replace winner = 1 if prtvtdee == 1 | prtvtdee == 3
replace winner = 0 if (winner ==. & prtvtdee >4 & prtvtdee <=11)  /*include "Others" because 2-party coalition*/
* Finland
replace winner = 1 if prtvtcfi == 1 | prtvtcfi == 13 | prtvtcfi == 14 | prtvtcfi == 12 | prtvtcfi == 2 | prtvtcfi == 5
replace winner = 0 if winner == . & prtvtcfi < 19
* France
replace winner = 1 if prtvtcfr == 9 | prtvtcfr == 12 | prtvtcfr == 7
replace winner = 0 if winner == . & prtvtcfr < 17
* Germany
replace winner = 1 if prtvdde2 == 2 | prtvdde2 == 4
replace winner = 0 if winner == . & prtvdde2 < 10
* Hungary
replace winner = 1 if prtvtdhu == 3
replace winner = 0 if winner ==. & prtvtdhu <66 /*include "Others" because no coalition government*/
* Iceland
replace winner = 1 if prtvtais == 1 | prtvtais == 4
replace winner = 0 if winner == . & prtvtais < 9
* Ireland
replace winner = 1 if prtvtaie == 2 | prtvtaie == 5
replace winner = 0 if winner == . & prtvtaie < 10
* Italy
replace winner = 1 if prtvtbit == 1 | prtvtbit == 8 | prtvtbit == 5 | prtvtbit == 6 | prtvtbit == 11
replace winner = 0 if winner == . & prtvtbit < 15
* Lithuania
replace winner = 1 if prtvalt1 == 3 | prtvalt1 == 7 | prtvalt1 == 8 | prtvalt1 == 9
replace winner = 0 if winner == . & prtvalt1 <55 /*include "Others" because 4-party coalition*/
* Norway
replace winner = 1 if prtvtano == 3 | prtvtano == 2 | prtvtano == 6
replace winner = 0 if winner == . & prtvtano < 11
* Poland
replace winner = 1 if prtvtcpl == 2 | prtvtcpl == 5
replace winner = 0 if winner == . & prtvtcpl <66 /*include "Others" because 2-party coalition*/
* Portugal
replace winner = 1 if prtvtbpt == 10 | prtvtbpt == 2
replace winner = 0 if winner == . & prtvtbpt < 14
* Slovakia
replace winner = 1 if prtvtcsk == 3
replace winner = 0 if winner == . & prtvtcsk <66 /*include "Others" because no coalition government*/
* Slovenia
replace winner = 1 if prtvtdsi == 1 | prtvtdsi == 3 | prtvtdsi == 4 | prtvtdsi == 5 | prtvtdsi == 8
replace winner = 0 if winner == . & prtvtdsi < 66 /*include "Others" because 5-party coalition*/
* Spain
replace winner = 1 if prtvtces == 1
replace winner = 0 if winner == . & prtvtces < 17
* Sweden
replace winner = 1 if prtvtbse == 5 | prtvtbse == 2 | prtvtbse == 1 | prtvtbse == 3
replace winner = 0 if winner == . & prtvtbse < 12
* Switzerland
replace winner = 1 if prtvtdch == 1 | prtvtdch == 2 | prtvtdch == 3 | prtvtdch == 4 | prtvtdch == 7
replace winner = 0 if winner == . & prtvtdch < 21
* Ukraine
replace winner = 1 if prtvtcua == 5
replace winner = 0 if winner == . & prtvtcua < 8 /*exclude "Others" because unclear whether part of government or not*/
* United Kingdom
replace winner = 1 if prtvtgb == 1 | prtvtgb == 3 
replace winner = 0 if winner == . & prtvtgb < 23

lab var winner "Voted for the winning camp"
lab val winner yn

drop name-proddate
drop cntry-supqyr

save ess2012.dta, replace


** Varieties-of-Democracy Project
use "[path]\V-Dem-CY-Full+Others-v9.dta"

drop if year <2009
keep v2xel_frefair year country_name country_id
save vdem_v9.dta, replace

*choose years here according to governments.xlsx
gen country = .
replace country=401 if country_name=="Japan" & year==2011
replace country=403 if country_name=="South Korea" & year==2011
replace country=405 if country_name=="Mongolia" & year==2010
replace country=406 if country_name=="Philippines" & year==2009
replace country=407 if country_name=="Taiwan" & year==2010
replace country=409 if country_name=="Indonesia" & year==2011
replace country=501 if country_name=="Argentina" & year==2012
replace country=502 if country_name=="Bolivia" & year==2013
replace country=503 if country_name=="Brazil" & year==2013
replace country=504 if country_name=="Chile" & year==2012
replace country=505 if country_name=="Colombia" & year==2013
replace country=506 if country_name=="Costa Rica" & year==2013
replace country=507 if country_name=="Dominican Republic" & year==2013
replace country=508 if country_name=="Ecuador" & year==2013
replace country=509 if country_name=="El Salvador" & year==2013
replace country=510 if country_name=="Guatemala" & year==2013
replace country=512 if country_name=="Mexico" & year==2013
replace country=514 if country_name=="Panama" & year==2013
replace country=515 if country_name=="Paraguay" & year==2013
replace country=516 if country_name=="Peru" & year==2013
replace country=517 if country_name=="Uruguay" & year==2013
replace country=701 if country_name=="Albania" & year==2012
replace country=703 if country_name=="Belgium" & year==2012
replace country=704 if country_name=="Bulgaria" & year==2012
replace country=706 if country_name=="Cyprus" & year==2012
replace country=707 if country_name=="Czech Republic" & year==2012
replace country=708 if country_name=="Denmark" & year==2013
replace country=709 if country_name=="Estonia" & year==2012
replace country=710 if country_name=="Finland" & year==2012
replace country=711 if country_name=="France" & year==2013
replace country=712 if country_name=="Germany" & year==2012
replace country=714 if country_name=="Hungary" & year==2012
replace country=715 if country_name=="Iceland" & year==2012
replace country=716 if country_name=="Ireland" & year==2012
replace country=718 if country_name=="Italy" & year==2013
replace country=721 if country_name=="Lithuania" & year==2013
replace country=725 if country_name=="Norway" & year==2012
replace country=726 if country_name=="Poland" & year==2012
replace country=727 if country_name=="Portugal" & year==2012
replace country=731 if country_name=="Slovakia" & year==2012
replace country=732 if country_name=="Slovenia" & year==2012
replace country=733 if country_name=="Spain" & year==2013
replace country=734 if country_name=="Sweden" & year==2012
replace country=735 if country_name=="Switzerland" & year==2012
replace country=737 if country_name=="Ukraine" & year==2013
replace country=738 if country_name=="United Kingdom" & year==2012 
drop if country==.
 
list country_name v2xel_frefair
sort country
rename year vdemyear
rename country_id vdemid
drop country_name
rename v2xel_frefair freefair
save vdem_v9.dta, replace


** merging survey data and V-Dem data
use .\ess2012.dta
append using .\asian3.dta
append using .\latino2013.dta
drop country
rename countrymerge country
drop numentre
drop dweight pweight idno
merge m:1 country using vdem_v9.dta
tab country if _merge==1
drop if _merge==1
save losing_trust.dta, replace

** prepare and convert data to Mplus
gen trust2a = trust2*100
gen trust3a = trust3*100
gen trust5a = trust5*100
gen trust6a = trust6*100
gen loser=1-winner
gen edu_no = .
gen edu_prim = .
gen edu_sec = .
gen edu_ter = .
replace edu_no = 1 if edulevel==1
replace edu_no = 0 if edu_no==. & edulevel!=.
replace edu_prim = 1 if edulevel==2
replace edu_prim = 0 if edu_prim==. & edulevel!=.
replace edu_sec = 1 if edulevel==3
replace edu_sec = 0 if edu_sec==. & edulevel!=.
replace edu_ter = 1 if edulevel==4
replace edu_ter = 0 if edu_ter==. & edulevel!=.
rename as1 satgov
replace satgov = as2 if satgov==.

save losing_trust.dta, replace

stata2mplus country trust2a trust3a trust5a trust6a loser dpe4 ///
spe2 polint soctrust status1 edu_no edu_prim edu_sec edu_ter female age ///
freefair satgov ///
using "[path]\losingtrust", missing(999) replace



*****************************************
				analyses
*****************************************
*descriptive statistics
use losing_trust.dta
sum trust2a trust3a trust5a trust6a loser dpe4 satgov spe2 polint soctrust status1 edu_no edu_prim edu_sec edu_ter female age freefair if loser!=. & female!=. & age!=. & status1!=. & spe2!=. & edu_no!=. & polint!=. & soctrust!=. & (trust2a!=. | trust3a!=. | trust5a!=. | trust6a!=. | dpe4!=. | satgov!=. )

//**do analyses in Mplus**//
*(1) multigroup cfa for trust measure: cfa_configural.inp, cfa_metric.inp*
*(2) model 0a, model 0b: baseline measurement models*
*(3) model 1: direct effect of losing on trust*
*(4) model 2: direct effect of losing on trust, controlling for perceptions of electoral fairness + government satisfaction*
*(5) model 3: indirect effects of losing on trust via government satisfaction + perceptions of electoral fairness*
*(6) model 4: cross-level interaction on path a of indirect effect via perceptions of electoral fairness; view plot and save plot data as model4.dat*
*(7) model 5: cross-level interaction on path a of indirect effect via government satisfaction; view plot and save plot data as model5.dat* 


*import plot data from Mplus and plot interaction effect
graph set window fontface "Calibri"
clear all
import delimited xaxis loser loser_low loser_high using "[path]\model4.dat", delimiter(tab, collapse) varnames(nonames) 
destring xaxis loser loser_low loser_high, force replace
save plot1.dta, replace
twoway ///
(line loser xaxis, lp(solid) lc(gs1)) ///
(line loser_low xaxis, lp(dash) lc(gs6)) ///
(line loser_high xaxis, lp(dash) lc(gs6)), ///
graphregion(color(white)) legend(region(lp(blank))) ///
ytitle("effects of losing on perceptions of electoral fairness") yline(0) ///
xtitle("electoral integrity") xlabel(0.3(0.1)1) ///
legend(off) name(plot1, replace)

ssc install overlay

use plot1.dta
/*manually add v2xel_frefair values as new variable*/
save plot3.dta, replace

twoway ///
(hist freefair, width(0.02) percent start(0.3) color(gs14) yaxis(2)) ///
(line loser xaxis, lp(solid) lc(gs1) yaxis(1)) ///
(line loser_low xaxis, lp(dash) lc(gs6) yaxis(1)) ///
(line loser_high xaxis, lp(dash) lc(gs6) yaxis(1)) , ///
graphregion(color(white)) legend(region(lp(blank))) ///
ytitle("effects of losing on perceptions of electoral fairness") yline(0) ///
xtitle("electoral integrity") xlabel(0.3(0.1)1) ///
legend(off) name(plot3, replace)


graph set window fontface "Calibri"
clear all
import delimited xaxis loser loser_low loser_high using "[path]\model5.dat", delimiter(tab, collapse) varnames(nonames) 
destring xaxis loser loser_low loser_high , force replace
save plot2.dta, replace
twoway ///
(line loser xaxis, lp(solid) lc(gs1)) ///
(line loser_low xaxis, lp(dash) lc(gs6)) ///
(line loser_high xaxis, lp(dash) lc(gs6)), ///
graphregion(color(white)) legend(region(lp(blank))) ///
ytitle("effects of losing on government satisfaction") yline(0) ///
xtitle("electoral integrity") xlabel(0.3(0.1)1) ///
legend(off) name(plot2, replace)